<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>v-on</title>
  <style>
    .box {
      width: 400px;
      height: 200px;
      margin: 0 auto;
    }
  </style>
</head>

<body>
  <div id='app'>
    <div class="box">
      <h3>FindYou售货机</h3>
      <button @click="fn(5)">可乐5元</button>
      <button @click="fn(10)">咖啡10元</button>
      <button @click="drink(1)">喝一瓶可乐</button>
      <button @click="drink(2)">喝一杯咖啡</button>
      <p>银行卡余额:{{ money }}元</p>
      <p>你现在有可乐: {{cole}}瓶, 咖啡: {{cafe}} 瓶</p>
    </div>

  </div>
  <script src="../vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        money: 100,
        cole: 0,
        cafe: 0
      },
      methods: {
        fn(a) {
          if (this.money - a < 0) {
            alert('余额不足')
            return
          }
          this.money -= a;
          if (a == 5) {
            this.cole++
          } else {
            this.cafe++
          }
        },
        drink(a) {
          if (a == 1) {//喝的是可乐
            if (this.cole >= 1) {
              this.cole--;
              alert('真好喝')
            } else {
              alert('没可乐啦，买点吧')
            }
          } else {//喝的是咖啡
            if (this.cafe >= 1) {
              alert('喝完精神好多啊')
              this.cafe--;
            } else {
              alert('没咖啡啦，买点吧')
            }
          }
        }
      }
    }) 
  </script>
</body>

</html>